use sqlx::postgres::PgPool;
use axum::response::{IntoResponse};
use axum::http::StatusCode;
use axum::extract::{Extension, Query};
use crate::error::user_error::user_error_map;
use crate::model::user_model;
use crate::types::user_types::*;
use crate::types::base_types::*;
use crate::error::base_error::Json;
use axum_client_ip::ClientIp;
use crate::handler::base::*; 

/// 用户登陆
pub async fn login_user(
    req: Query<LoginUserReq>,
    ClientIp(ip): ClientIp,
    Extension(pool): Extension<PgPool>,
) -> impl IntoResponse {
    println!("ip:{}",ip);

    let hash_password = encode_password(&req.password).await;

    println!("hash:{:#?}",hash_password.to_string());

    match user_model::select_user_login(&pool, &req.phone, &hash_password.to_string(), &req.country).await {
        Ok(result) => {
            (StatusCode::OK, Json(BaseGetData::ok(result)))
        }
        Err(e) => {
            (StatusCode::BAD_REQUEST, Json(BaseGetData::err(user_error_map(e).await,false)))
        }
    }
}